package com.futureCloudMOM.core.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.futureCloudMOM.common.utils.SecurityUtils;
import com.futureCloudMOM.core.domain.MesBarcodeinfo;
import com.futureCloudMOM.core.domain.ProduceDispatch;
import com.futureCloudMOM.core.domain.ProduceReport;
import com.futureCloudMOM.core.mapper.MesBarcodeinfoMapper;
import com.futureCloudMOM.core.service.MesBarcodeinfoService;
import com.futureCloudMOM.core.service.ProduceDispatchService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
* MES条码业务层
*/
@Service
@AllArgsConstructor
public class MesBarcodeinfoServiceImpl extends ServiceImpl<MesBarcodeinfoMapper, MesBarcodeinfo>
implements MesBarcodeinfoService{

    // 派工单
    private final ProduceDispatchService produceDispatchService;

    @Override
    @Transactional
    public void createdMesBarcode(ProduceReport produceReport) {
        // 根据标包数量和报工数量计算要生成的条码数量
        BigDecimal[] barcode = produceReport.getQty().divideAndRemainder(produceReport.getMinPackage());
        // 根据报工单里面的派工单ID获取物料ID
        ProduceDispatch produceDispatch = produceDispatchService.getById(produceReport.getDispatchID());
        // 生成条码号
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        StringBuilder no = new StringBuilder().append(simpleDateFormat.format(new Date())).append(produceReport.getDispatchID());
        // 条码批次
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyyMMdd");
        // 生成条码
        for (int i = 1; i <= barcode[0].intValue(); i++) {
            MesBarcodeinfo mesBarcodeinfo = new MesBarcodeinfo();
            mesBarcodeinfo.setBarcode(no.toString() + String.format("%03d",i));
            mesBarcodeinfo.setMaterialid(produceDispatch.getMaterialId());
            mesBarcodeinfo.setStatus(0L);
            // 条码数量等于标包数量
            mesBarcodeinfo.setQuantity(produceReport.getMinPackage());

            mesBarcodeinfo.setBatchnum(simpleDateFormat1.format(new Date()));
            mesBarcodeinfo.setCreatedate(new Date());
            mesBarcodeinfo.setOperator(SecurityUtils.getUsername());
            mesBarcodeinfo.setReportid(produceReport.getId());
            mesBarcodeinfo.setBillid(produceReport.getDispatchID());
            mesBarcodeinfo.setMinpackage(produceReport.getMinPackage());
            mesBarcodeinfo.setResourceid(produceReport.getResourceId());
            mesBarcodeinfo.setProcessid(produceReport.getProcessID());
            // 新增条码
            save(mesBarcodeinfo);
        }
        // 余数大于零 则生成余量条码
        if(barcode[1].compareTo(BigDecimal.ZERO) > 0){
            MesBarcodeinfo mesBarcodeinfo = new MesBarcodeinfo();
            mesBarcodeinfo.setBarcode(no.append(barcode[0].intValue() + 1).toString());
            mesBarcodeinfo.setMaterialid(produceDispatch.getMaterialId());
            mesBarcodeinfo.setStatus(0L);
            // 余量
            mesBarcodeinfo.setQuantity(barcode[1]);
            mesBarcodeinfo.setBatchnum(simpleDateFormat1.format(new Date()));
            mesBarcodeinfo.setCreatedate(new Date());
            mesBarcodeinfo.setOperator(SecurityUtils.getUsername());
            mesBarcodeinfo.setReportid(produceReport.getId());
            mesBarcodeinfo.setBillid(produceReport.getDispatchID());
            mesBarcodeinfo.setMinpackage(produceReport.getMinPackage());
            mesBarcodeinfo.setResourceid(produceReport.getResourceId());
            mesBarcodeinfo.setProcessid(produceReport.getProcessID());
            // 新增余量条码
            save(mesBarcodeinfo);
        }
    }


}
